# 单线程版本;takes:80.72s,76s；average_takes:78s
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtWidgets
from PyQt5.QtCore import QThread,pyqtSignal,Qt
import sys
from utils import *
from win import Ui_Win_main
from info import Ui_Win_info
from dressing import Ui_Dressing
class Worker(QThread):
    sinOut = pyqtSignal(list)
    def __int__(self):
        super().__init__()
        self.video_list_path=None
    def setDir(self,video_list_path):
        self.video_list_path=video_list_path
    def run(self):
        save_dir=os.path.join(self.video_list_path,"..","result")
        if not os.path.exists(save_dir):os.makedirs(save_dir)
        count=0
        length=len(os.listdir(self.video_list_path))
        for single_video_path in os.listdir(self.video_list_path):
            count+=1
            single_video_path=os.path.join(self.video_list_path,single_video_path)
            name=dress_single_video(single_video_path,save_dir)
            list1=[name,count,length]
            self.sinOut.emit(list1)
        time.sleep(0.5)
        self.sinOut.emit(["done",1,1])

class MyDressing(QMainWindow, Ui_Dressing):
    def __init__(self):
        super(MyDressing, self).__init__()
        self.setupUi(self)
        self.video_list_path=None
        self.worker=Worker()
        self.worker.sinOut.connect(self.setValue)
        self.start_time=time.time()
    def setDir(self,video_list_path):
        self.video_list_path=video_list_path
    def worker_start(self):
        self.worker.setDir(self.video_list_path)
        self.worker.start()
    def setValue(self,list1):
        name,count,length=list1
        self.label.setText(name)
        self.label_2.setText("takes "+str(round(time.time()-self.start_time,2))+"s")
        if name=="done":
            self.close()
        else:
            self.progressBar.setMaximum(length)
            self.progressBar.setValue(count)

            
class MyInfoWin(QMainWindow, Ui_Win_info):
    def __init__(self):
        super(MyInfoWin, self).__init__()
        self.setupUi(self)
class Window(QMainWindow, Ui_Win_main):
    def __init__(self):
        super(Window, self).__init__()
        self.setupUi(self)
        self.infoWin=MyInfoWin()
        self.src_path=""
        self.btn_info.clicked.connect(self.showInfo)
        self.btn_1.clicked.connect(self.change_dir)
        self.btn_2.clicked.connect(self.go)
    def change_dir(self):
        self.src_path = QtWidgets.QFileDialog.getExistingDirectory(None,"视频文件夹所在文件夹","./")
        self.lineEdit.setText(self.src_path)
    def go(self):
        if self.radioButton_2.isChecked():
            self.dressingBar=MyDressing()
            self.dressingBar.setDir(self.src_path)
            self.dressingBar.progressBar.setValue(0)
            self.dressingBar.label.setText("。。。")
            self.dressingBar.show()
            self.dressingBar.worker_start()
            # dress_videos(self.src_path)
        else:
            dress_a_video(self.src_path)
    def showInfo(self):
        self.infoWin.show()

if __name__ == '__main__':
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    app = QApplication(sys.argv)
    father = Window()
    father.show()
    sys.exit(app.exec_())


